//
//  LSZDynamicHomepageCollectionView.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/22.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZDynamicHomepageCollectionView.h"

@interface LSZDynamicHomepageCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) NSArray * arrayName;
@property(nonatomic,strong) LSZDynamicClassificationManager * dynamicClassificationManager;
@end

@implementation LSZDynamicHomepageCollectionView

static NSString * const reuseIdentifier = @"Cell";
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.backgroundColor = [UIColor whiteColor];
        [self registerClass:[LSZDynamicHomepageCollCell class] forCellWithReuseIdentifier:@"LSZDynamicHomepageCollCell"];
        self.backgroundColor = RGBColor(212, 212, 212);
    }
    return self;
}
-(void)setClassManager:(LSZDynamicClassificationManager *)classManager
{
   self.arrayName = classManager.mutNameArray;
    self.dynamicClassificationManager = classManager;
    [self reloadData];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.arrayName.count >6) {
        return 8;
    }else{
        return self.arrayName.count+2;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LSZDynamicHomepageCollCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZDynamicHomepageCollCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.labelTitle.text = @"话题";
        cell.imageViewCover.image = [UIImage imageNamed:[NSString stringWithFormat:@"动态_热议榜"]];
    }else{
        if((self.arrayName.count >6&&indexPath.row == 7)||(self.arrayName.count <6&&indexPath.row == self.arrayName.count+1)){
            cell.labelTitle.text = @"更多";
            cell.imageViewCover.image = [UIImage imageNamed:[NSString stringWithFormat:@"动态_其他"]];
        }
        else{
            cell.labelTitle.text = self.arrayName[indexPath.row-1];
            LSZDynamicClassificationMg * mg = nil;
            mg = self.dynamicClassificationManager.mutMgArray[indexPath.row-1];
            if (mg.logo_main.count >0) {
                if (ScreenWidth <= 375) {
                    NSString *decodeString = [mg.logo_main[0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    NSLog(@"%@>>>>>1",decodeString);
                    [cell.imageViewCover sd_setImageWithURL:[NSURL URLWithString:decodeString] placeholderImage:[UIImage imageNamed:@"placeholder"]];
                }else {
                    NSString *decodeString = [mg.logo_main[1] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    [cell.imageViewCover sd_setImageWithURL:[NSURL URLWithString:decodeString] placeholderImage:[UIImage imageNamed:@"placeholder"]];
                }
            }else{
                cell.imageViewCover.image = [UIImage imageNamed:@"placeholder"];
            }
        }
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_pushBlock != nil) {
        _pushBlock(indexPath.row);
    }
}
@end
